package com.orangelife.service.prinitConfig.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.prinitConfig.IPrinitConfigService;
import com.orangelife.service.prinitConfig.model.PrinitConfigDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PrinitConfig类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-12-08 17:27:34
 * ==============================================================
 */
@Service("prinitConfigService")
public class PrinitConfigServiceImpl extends MybaitsGenericDaoSubLogic
		implements IPrinitConfigService {
	private static Logger logger = Logger
			.getLogger(PrinitConfigServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PrinitConfigServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	@CacheEvict(value = { "bauthPrinitConfig", "bauthPrinitConfigs",
			"prinitConfigInfos" }, allEntries = true)
	public int addPrinitConfig(PrinitConfigDto prinitConfig)
			throws AppBizException {

		Assert.notNull(prinitConfig);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(prinitConfig.getTempleteName())) {
			if (prinitConfig.getTempleteName().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "模版类型长度超出");
			}
		}

		if (!StringUtils.isBlank(prinitConfig.getPrintInfo())) {
			if (prinitConfig.getPrintInfo().length() > 128) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "配置名称长度超出");
			}
		}

		if (!StringUtils.isBlank(prinitConfig.getPrintType())) {
			if (prinitConfig.getPrintType().length() > 255) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"1  web   2 lod长度超出");
			}
		}

		if (!StringUtils.isBlank(prinitConfig.getIsDefalut())) {
			if (prinitConfig.getIsDefalut().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"0  否 1 是长度超出");
			}
		}

		if (!StringUtils.isBlank(prinitConfig.getImgUrl())) {
			if (prinitConfig.getImgUrl().length() > 2056) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "长度超出");
			}
		}

		if (!StringUtils.isBlank(prinitConfig.getRemark())) {
			if (prinitConfig.getRemark().length() > 512) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "备注长度超出");
			}
		}
		if (!StringUtils.isBlank(prinitConfig.getIsDefalut())) {
			if (prinitConfig.getIsDefalut().equals("1")) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("templeteName", prinitConfig.getTempleteName());
				map.put("isDefalut", "0");
				map.put("printType", prinitConfig.getPrintType());
				map.put("commID", prinitConfig.getCommID());
				this.getSqlSession().update(
						"PrinitConfigMapper.bauthEditDeFalt", map);
			}
		}
		prinitConfig.setConfigID(IdUtil.getuuId());
		prinitConfig.setItemStat(TypeConst.FLAG_ON);
		prinitConfig.setTscreate(new Date());
		prinitConfig.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PrinitConfigMapper.insertSelective", prinitConfig);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	@CacheEvict(value = { "bauthPrinitConfig", "bauthPrinitConfigs",
			"prinitConfigInfos" }, allEntries = true)
	public int editPrinitConfig(PrinitConfigDto prinitConfig)
			throws AppBizException {

		Assert.notNull(prinitConfig);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(prinitConfig.getConfigID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PrinitConfigDto oldPrinitConfig = this
				.findPrinitConfigByID(prinitConfig.getConfigID());

		if (!StringUtils.isBlank(prinitConfig.getTempleteName())) {
			if (prinitConfig.getTempleteName().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "模版类型长度超出");
			}
		}

		if (!StringUtils.isBlank(prinitConfig.getPrintInfo())) {
			if (prinitConfig.getPrintInfo().length() > 128) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "配置名称长度超出");
			}
		}

		if (!StringUtils.isBlank(prinitConfig.getPrintType())) {
			if (prinitConfig.getPrintType().length() > 255) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"1  web   2 lod长度超出");
			}
		}

		if (!StringUtils.isBlank(prinitConfig.getIsDefalut())) {
			if (prinitConfig.getIsDefalut().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"0  否 1 是长度超出");
			}
		}

		if (!StringUtils.isBlank(prinitConfig.getImgUrl())) {
			if (prinitConfig.getImgUrl().length() > 2056) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "长度超出");
			}
		}

		if (!StringUtils.isBlank(prinitConfig.getRemark())) {
			if (prinitConfig.getRemark().length() > 512) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "备注长度超出");
			}
		}

		if (oldPrinitConfig == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		prinitConfig.setTsedit(new Date());
		return this.getSqlSession().update(
				"PrinitConfigMapper.updateByPrimaryKeySelective", prinitConfig);
	}

	@Override
	public PrinitConfigDto findPrinitConfigByID(String prinitConfigID)
			throws AppBizException {
		if (StringUtils.isBlank(prinitConfigID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PrinitConfigDto oldPrinitConfig = this.getSqlSession().selectOne(
				"PrinitConfigSearchMapper.selectByPrimaryKey", prinitConfigID);
		if (oldPrinitConfig == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPrinitConfig.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPrinitConfig;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	@CacheEvict(value = { "bauthPrinitConfig", "bauthPrinitConfigs",
			"prinitConfigInfos" }, allEntries = true)
	public int deletePrinitConfigByLogic(String prinitConfigID)
			throws AppBizException {

		if (StringUtils.isBlank(prinitConfigID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PrinitConfigDto oldPrinitConfig = this
				.findPrinitConfigByID(prinitConfigID);

		oldPrinitConfig.setItemStat(TypeConst.FLAG_OFF);
		oldPrinitConfig.setTsedit(new Date());
		return this.getSqlSession().update(
				"PrinitConfigMapper.updateByPrimaryKeySelective",
				oldPrinitConfig);
	}

	@Override
	@Cacheable(value = "prinitConfigInfos")
	public List<Map<String, Object>> findPrinitConfigByPage(String commID,
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("commID", commID);
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PrinitConfigSearchMapper.findAllPrinitConfig", map);
		return list;
	}

	@Override
	public List<PrinitConfigDto> getPrinitConfigByPage(String tsCreate,
			String search, int pageNo, int pageSize) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PrinitConfigDto> list = this.getSqlSession().selectList(
				"PrinitConfigSearchMapper.getAllPrinitConfigDto", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPrinitConfigByRelevantID(
			String configID, String commID, String printType)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("configID", configID);
		map.put("commID", commID);
		map.put("printType", printType);

		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PrinitConfigSearchMapper.selectPrinitConfigDtoyRelevantID",
				map);
		return list;
	}

	@Override
	public List<PrinitConfigDto> findPrinitConfigByRelevantID(String configID,
			String commID, String printType) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("configID", configID);
		map.put("commID", commID);
		map.put("printType", printType);

		List<PrinitConfigDto> list = this
				.getSqlSession()
				.selectList(
						"PrinitConfigSearchMapper.findPrinitConfigDtoByRelevantID",
						map);
		return list;
	}

	@Override
	@Cacheable(value = "bauthPrinitConfig")
	public List<PrinitConfigDto> bauthfindPrinitConfig(
			List<String> prinitConfigIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PrinitConfigDto> list = this.getSqlSession().selectList(
				"PrinitConfigSearchMapper.batchFindPrinitConfig",
				prinitConfigIDs);
		return list;
	}

	@Override
	@Cacheable(value = "bauthPrinitConfigs")
	public List<Map<String, Object>> bauthfindPrinitConfigByMap(
			Map<String, Object> prinitConfig) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PrinitConfigSearchMapper.batchFindPrinitConfigByMap",
				prinitConfig);
		return list;
	}

	@Override
	@CacheEvict(value = { "bauthPrinitConfig", "bauthPrinitConfigs",
			"prinitConfigInfos" }, allEntries = true)
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int setToDefault(PrinitConfigDto prinitConfig)
			throws AppBizException {
		PrinitConfigDto oldprinitConfig = this
				.findPrinitConfigByID(prinitConfig.getConfigID());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("templeteName", prinitConfig.getTempleteName());
		map.put("isDefalut", "0");
		map.put("printType", oldprinitConfig.getPrintType());
		map.put("commID", oldprinitConfig.getCommID());
		this.getSqlSession().update("PrinitConfigMapper.bauthEditDeFalt", map);
		prinitConfig.setItemStat(TypeConst.FLAG_ON);
		prinitConfig.setTscreate(new Date());
		prinitConfig.setTsedit(new Date());
		prinitConfig.setIsDefalut("1");
		return this.editPrinitConfig(prinitConfig);
	}
}